package com.cslg.ac.demo.controller.auth.phone;

import com.cslg.ac.demo.client.JsonReqClient;
import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.service.auth.phone.PhoneService;
import com.cslg.ac.demo.util.string.RandomCodeUtil;
import com.cslg.ac.demo.util.communicate.ResponseUtil;
import com.cslg.ac.demo.util.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

@Controller
@PropertySource(value = {"classpath:auth.properties"},encoding="utf-8")
public class phoneController {
    @Autowired
    PhoneService phoneService;
    @Autowired
    JsonReqClient jsonReqClient;
    @Value("${sid}")
    String sid;
    @Value("${token}")
    String token;
    @Value("${appid}")
    String appid;
    @Value("${templateid}")
    String templateid;
    @Value("${phoneLength}")
    int phoneLength;
    @Value("${codeLength}")
    int codeLength;
    @Value("${time}")
    int time;


    /**
     *在注册前就验证
     * @param phone
     * @param httpSession
     * @param out
     */
    @RequestMapping(value = "/user/authPhone",method = RequestMethod.POST)
    public void authPhone(@RequestParam("phone")String phone, HttpSession httpSession, PrintWriter out){
        if(phone.length() != phoneLength || !StringUtil.allNumber(phone)){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        String param = RandomCodeUtil.getCode();
        httpSession.setAttribute("code",param);
        httpSession.setAttribute("phone",phone);
//        try {
//           int code =  phoneService.authPhone(phone,param,token,sid,appid,templateid);
//            ResponseUtil.responseCode(out,code);
//        } catch (Exception e) {
//            e.printStackTrace();
//            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
//             return;
//        }
        ResponseUtil.responseCode(out,ResponseCode.REQUEST_SUCCESSED);

    }

    @RequestMapping(value = "/user/authSuccess",method = RequestMethod.POST)
    public void authSucceed(@RequestParam("phone")String phone,@RequestParam("code")String code,@RequestParam("fromTime") String fromTime,HttpSession httpSession,PrintWriter out){
//        if(code.length() != codeLength){
//            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
//            return;
//        }
//
//        Object phoneCodeo = httpSession.getAttribute("code");
//        if(phoneCodeo == null){
//            ResponseUtil.responseCode(out,ResponseCode.NOT_LOGON);
//            return;
//        }
//        String phoneCode = phoneCodeo.toString();
//        if( phoneCode == null || !phoneCode.equals(code)){
//            ResponseUtil.responseCode(out,ResponseCode.PARAM_ERROR);
//            return;
//        }
//        String getPhone = (String) httpSession.getAttribute("phone");
//        if(phone == null || !getPhone.equals(phone)){
//            ResponseUtil.responseCode(out,ResponseCode.PARAM_ERROR);
//            return;
//        }
//        try {
//            SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String toTime = simpleFormat.format(new Date());
//            long from = simpleFormat.parse(fromTime).getTime();
//            long to = simpleFormat.parse(toTime.toString()).getTime();
//            int minutes = (int) ((to - from)/(1000 * 60));
//            if(minutes > time){
//                ResponseUtil.responseCode(out,ResponseCode.TIME_INVALID);
//                return;
//            }
//               int code1 =  phoneService.authSuccess(phone);
//
//            ResponseUtil.responseCode(out,code1);
//
//        } catch (Exception e) {
//              e.printStackTrace();
//            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
//        }

            int code1 = -1;
        try {
            code1 =  phoneService.authSuccess(phone);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
    }
        ResponseUtil.responseCode(out,code1);
        httpSession.removeAttribute("phone");
    }
}
